
package com.jf.cloud.service.sysmanager.remote;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jf.cloud.service.sysmanager.common.base.AjaxPageData;
import com.jf.cloud.service.sysmanager.common.base.OPaging;
import com.jf.cloud.service.sysmanager.common.db.Query;
import com.jf.cloud.service.sysmanager.common.util.StringUtil;
import com.jf.cloud.service.sysmanager.constant.PubgConstant;
import com.jf.cloud.service.sysmanager.dao.IPubgClubService;
import com.jf.cloud.service.sysmanager.dao.IPubgUserClubService;
import com.jf.cloud.service.sysmanager.dao.IPubgUserService;
import com.jf.cloud.service.sysmanager.dao.IPubgUserTeamService;
import com.jf.cloud.service.sysmanager.entity.PubgClub;
import com.jf.cloud.service.sysmanager.entity.PubgUser;
import com.jf.cloud.service.sysmanager.entity.PubgUserClub;
import com.jf.cloud.service.sysmanager.entity.PubgUserTeam;
import com.jf.cloud.service.sysmanager.support.entity.Result;
import com.jf.cloud.service.sysmanager.support.web.BaseController;

/**
 * 员工控制器
 * 
 * @author 代码机1.0 创建时间：2018-03-31
 */
@Controller
@RequestMapping("/pubgUser")
public class PubgUserController extends BaseController {

	/**
	 * 日志
	 */
	private static Log log = LogFactory.getLog(PubgUserController.class);

	/**
	 * 员工服务
	 */
	@Resource
	private IPubgUserService pubgUserService;

	@Resource
	private IPubgClubService pubgClubService;

	@Resource
	private IPubgUserClubService pubgUserClubService;
	
	@Resource
	private IPubgUserTeamService pubgUserTeamService;

	/**
	 * 获取用户列表数据
	 * 
	 * @author 朱建文 创建时间：2018年4月17日 下午5:11:56
	 */
	@ResponseBody
	@RequestMapping("/data")
	public AjaxPageData<PubgUser> getUserPageList(OPaging<PubgUser> page, HttpServletRequest request){
		AjaxPageData<PubgUser> dataPage = new AjaxPageData<PubgUser>();
		try {
			page.setOrderBy(" ORDER BY REG_TIME DESC ");
			dataPage = getAjaxPageData(pubgUserService.findPage(page));
		} catch (Exception e) {
			log.error("获取员工列表数据出错！", e);
		}
		return dataPage;
	}

	/**
	 * 获取指定用户的信息
	 * 
	 * @author 朱建文 创建时间：2018年4月17日 下午5:39:08
	 */
	@ResponseBody
	@RequestMapping("/getUserDetail")
	public Result<Map<String, Object>> getUserDetail(Long rowId, HttpServletRequest request) {
		Result<Map<String, Object>> result = new Result<Map<String, Object>>();
		result.setCode(-1);
		result.setMessage("获取用户信息失败！");
		try {
			Map<String, Object> dataMap = this.pubgUserService.getUserInfoDetail(rowId);
			result.setCode(0);
			result.setData(dataMap);
			result.setMessage("获取用户信息成功！");
		} catch (Exception e) {
			log.error("获取指定用户的信息出错！", e);
		}
		return result;
	}
	
	/**
	 * 验证用户信息 是否可以设置为部长
	 *
	 * @author 高冬冬 2018年3月31日
	 */
	@ResponseBody
	@RequestMapping("/checkPubgUserInfo")
	public Map<String, Object> checkPubgUserInfo(HttpServletRequest request, Long clubId, String userAccountArray) {
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("code", "-1");
		List<PubgUser> userList = new ArrayList<PubgUser>();
		try {
			if (StringUtil.isNotEmpty(userAccountArray)) {
				String[] userAccountList = userAccountArray.split(",");
				for (int index = 0;index < userAccountList.length;index++) {
					if (clubId != null && clubId > 0){
						PubgClub pubgClub = pubgClubService.findByPK(clubId);
						if (pubgClub.getMinisterName().contains(userAccountList[index])) {
							continue;
						}
					}
					Query query = new Query();
					query.put("USER_ACCOUNT", userAccountList[index]);
					userList = pubgUserService.findList(query);
					if (userList != null && userList.size() == 1) {
						PubgUser pubgUser = userList.get(0);
						if (pubgUser.getUserRole() != 10) {
							result.put("code", "-2");
							result.put("message", "第"+(index+1)+"个用户角色不是普通用户，无法设置为部长！");
							return result;
						}

						if (pubgUser.getBalance() != 0) {
							result.put("code", "-2");
							result.put("message", "第"+(index+1)+"个用户鸡蛋余额不为0，无法设置为部长！");
							return result;
						}
						query = new Query();
						query.put("USER_ID", pubgUser.getRowId());
						query.put("_NOT_EQUAL$RELATION_STATE", PubgConstant.USER_CLUB_RELATION_STATE_4);
						List<PubgUserClub> userClubList = pubgUserClubService.findList(query);//俱乐部关系
						if (userClubList != null && userClubList.size() > 0) {
							result.put("code", "-2");
							result.put("message", "第"+(index+1)+"个用户存在俱乐部关系，请先移除后再添加为管理员！");
							return result;
						}

						query = new Query();
						query.put("USER_ID", pubgUser.getRowId());
						query.put("_NOT_EQUAL$RELATION_STATE", PubgConstant.USER_TEAM_RELATION_STATE_4);
						List<PubgUserTeam> userTeamList = pubgUserTeamService.findList(query);//战队关系
						if (userTeamList != null && userTeamList.size() > 0) {
							result.put("code", "-2");
							result.put("message", "第"+(index+1)+"个用户存在战队关系，请先移除后再添加为管理员！");
							return result;
						}
					} else {
						result.put("code", "-2");
						result.put("message", "第"+(index+1)+"个用户不存在！");
						return result;
					}
				}
			}
			result.put("code", "0");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
}
